package com.xuzao.developer.service.impl;

import com.github.pagehelper.PageInfo;
import com.xuzao.developer.base.model.SysPower;
import com.xuzao.developer.base.model.SysResource;
import com.xuzao.developer.dao.SysPowerDao;
import com.xuzao.developer.dao.SysResourceDao;
import com.xuzao.developer.dto.SysPowerDTO;
import com.xuzao.developer.service.SysPowerService;
import com.xuzao.developer.tools.entity.Pager;
import com.xuzao.developer.vo.BaseResultVo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;


/**
 * @author xuzao
 * @description
 * @date 2019-09-19 14:32:23
 **/
@Service("sysPowerService")
public class SysPowerServiceImpl implements SysPowerService {

    @Resource
    private SysPowerDao sysPowerDao;
    @Resource
    private SysResourceDao sysResourceDao;

    @Override
    public PageInfo<SysPower> list(SysPowerDTO dto, Pager pager) {
        return sysPowerDao.list(dto, pager);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public BaseResultVo insert(Long[] sysResourceIds, Long userId) {
        SysPower sysPower = new SysPower();
        sysPower.setUserId(userId);
        sysPowerDao.delete(sysPower);

        if (null != sysResourceIds && sysResourceIds.length > 0) {
            for (Long sysResourceId : sysResourceIds) {
                SysResource sysResource = sysResourceDao.selectById(sysResourceId);
                SysPower insertSysPower = new SysPower();
                insertSysPower.setUserId(userId);
                insertSysPower.setUrl(sysResource.getUrl());
                sysPowerDao.insert(insertSysPower);
            }
        }
        return BaseResultVo.success();
    }

    @Override
    public BaseResultVo update(SysPowerDTO dto) {
        SysPower sysPower = new SysPower();
        //TODO 赋值
        Integer count = sysPowerDao.updateSelectiveById(sysPower);
        if (count > 0) {
            return BaseResultVo.success();
        }
        return BaseResultVo.error();
    }

    @Override
    public BaseResultVo get(SysPowerDTO dto) {
        SysPower sysPower = new SysPower();
        return BaseResultVo.success(sysPowerDao.selectById(sysPower.getId()));
    }
}